/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbean.MrpAtp;

import java.util.Collection;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import merlion.common.util.Consts;
import merlion.mrp.atpm.entity.PendingATP;
import merlion.mrp.atpm.session.ATPSessionBeanLocal;

/**
 *
 * @author Zhang Ying
 */
@ManagedBean
@ViewScoped
public class ATPSelectManagerBean {

    public static final String SELECTED_ATP_SO = "selectedAtpSo";
    public static final String SELECTED_ATP_RFQ = "selectedAtpRfq";
    @EJB
    private ATPSessionBeanLocal atpSession;
    private String pendingStrId;
    private PendingATP selectedATP;
    private Collection<PendingATP> pendingATPForSOs;
    private Collection<PendingATP> pendingATPForRFQs;

    /** Creates a new instance of ATPSelectManagerBean */
    public ATPSelectManagerBean() {
        
    }

    public String redirectToATPForRFQ() {
        if (pendingStrId == null) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful", "Please select atp."));
            return null;
        }
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(SELECTED_ATP_RFQ);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(SELECTED_ATP_SO);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(SELECTED_ATP_RFQ, selectedATP);
        return getRedirectPath(Consts.SIMULATE_FOR_RFQ_PATH+"Detail");
    }

    public String redirectToATPForSo() {
        if (pendingStrId == null) {
            FacesContext.getCurrentInstance().addMessage(null,
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Unsuccessful", "Please select atp."));
            return null;
        }
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(SELECTED_ATP_SO);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().remove(SELECTED_ATP_RFQ);
        FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put(SELECTED_ATP_SO, selectedATP);
        return getRedirectPath(Consts.SIMULATE_FOR_SO_PATH+"Detail");
    }

    public PendingATP getSelectedATP() {
        return selectedATP;
    }

    public void setSelectedATP(PendingATP selectedATP) {
        this.selectedATP = selectedATP;
        this.setPendingStrId(selectedATP.getPendingStrId());
    }

    public String getPendingStrId() {
        return pendingStrId;
    }

    public void setPendingStrId(String pendingStrId) {
        this.pendingStrId = pendingStrId;
    }

    public Collection<PendingATP> getPendingATPForRFQs() {
        if (pendingATPForRFQs == null) {
            pendingATPForRFQs = atpSession.getPendingATPForRFQs();
        }
        return pendingATPForRFQs;
    }

    public void setPendingATPForRFQs(Collection<PendingATP> pendingATPForRFQs) {
        this.pendingATPForRFQs = pendingATPForRFQs;
    }

    public Collection<PendingATP> getPendingATPForSOs() {
        if (pendingATPForSOs == null) {
            pendingATPForSOs = atpSession.getPendingATPForSos();
        }
        return pendingATPForSOs;
    }

    public void setPendingATPForSOs(Collection<PendingATP> pendingATPForSOs) {
        this.pendingATPForSOs = pendingATPForSOs;
    }

    private String getRedirectPath(String path) {
        return path + "?faces-redirect=true";
    }
}
